package ac.uk.qmul.gemom.scenario;

import java.util.Random;

/* 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */

public class Util {
	
private static Random r = new Random();  
	
	public static String getString(){
		
		int n=(int)(Math.random()*20)+4;
		
		char[] text=new char[n];
		
		int c='A';
		
		int r1=0;
		for(int i=0; i<n; i++)
		{
			r1 = (int)(Math.random()*3);
			
			switch(r1){
			
			case 0: c='0'+(int)(Math.random()*10); break;
			case 1: c='a'+ (int)(Math.random()*26); break;
			case 2: c='A'+ (int)(Math.random()*26); break;
			
			}
			text[i]=(char)c;
		}
		
		return new String(text);
	}
	
	public static String getValue(){
		
		int n=(int)(Math.random()*20)+4;
		
		char[] text=new char[n];
		
		int c='A';
		
		int r1=0;
		for(int i=0; i<n; i++)
		{
			r1 = (int)(Math.random()*3);
			
			switch(r1){
			
			case 0: c='0'+(int)(Math.random()*10); break;
			}
			text[i]=(char)c;
		}
		
		return new String(text);
	}
	
	public static String getNameString(int len, int distribution, double std_deviation){
		
		int n=0;
		// constant message length
		if(distribution == 0){
		    n=len;
		}
		
		// uniform distribution, currently standard 
        if(distribution == 1){
			
		    n=(int)(Math.random()*len*2);
		    if(n == 0)
		    	n++;

		}
        
        if(distribution == 2){
			
		    n =  (int)(r.nextGaussian()*std_deviation + len);
		    if(n<0)
		    	n=0;
		}
        
        char [] text=new char[n];
        
        int c='A';
		
	    int r1=0;
	    for(int i=0; i<n; i++)
	    {
		    r1 = (int)(Math.random()*3);
		
		    switch(r1){
		
		    case 0: c='0'+(int)(Math.random()*10); break;
		    case 1: c='a'+ (int)(Math.random()*26); break;
		    case 2: c='A'+ (int)(Math.random()*26); break;
		
		    }
		    text[i]=(char)c;
	    }
		
		return new String(text);
	}
	
	public static String getValueString(int len, int distribution, double std_deviation){
		
		int n=0;
		// constant message length
		if(distribution == 0){
		    n=len;
		}
		
		// uniform distribution, currently standard 
        if(distribution == 1){
			
		    n=(int)(Math.random()*len*2);
		    if(n == 0)
		    	n++;

		}
		
		char[] text=new char[n];
		
		int c='A';
		
		int r1=0;
		for(int i=0; i<n; i++)
		{
			c='0'+(int)(Math.random()*10);

			text[i]=(char)c;
		}
		
		return new String(text);
	}
	
	
	public static String getValueStringDouble(int max, int distribution, double std_deviation){
		
		double n=0;
		// constant message length
		if(distribution == 0){
		    n=Math.random()*max;
		}
		
		// uniform distribution, currently standard 
        if(distribution == 1){
			
		    n=Math.random()*max*2;

		}
	 
		return ""+n;
	}
	
	// change the message rate and delay to the real y parameter
	// R=
	public static double TransferRate(int Rate, int delay)
	{
		double y=0.0;
		
		y=(-Math.log10(0.5)*Rate*delay)/(60*1000);
		
		return y;
	}
	
	// get the next publish time which has the time interval follows an exponential distribution, returns in seconds
	public static double getNextPublishTimeExp(int rate)
	{
		
	    Random r = new Random();  
		
     	double uniRnd=r.nextDouble();
	    while(uniRnd==0)
	    {
		    uniRnd=r.nextDouble();
	    }
		double mPerSecond = ((double)rate)/60;
		return (-1.0/mPerSecond)*Math.log(uniRnd);
	}
	
	// get the next publish time which is uniform to the rate, returns in seconds
	public static double getNextPublishTimeUni(int rate)
	{
		return 60.0/((double)rate);
	}
	
	// get the next publish time which has the time interval follows an exponential distribution, returns in seconds
	public static double getDelayExpRnd(double mean, double uniRnd)
	{
		//mean is also measured in seconds, the variance will be the square
		double lambda = 1/mean; 
		return (-1.0/lambda)*Math.log(uniRnd);
	}
	
	
    public static String stripSlash(String text){
    	if(text.startsWith("/")){
    		text = text.substring(1,text.length());
    	}
    	if(text.endsWith("/")){
    		text = text.substring(0,text.length()-1);
    	}
        return text;
    	
    }
    
    public static String addSlash(String text){
    	if(!text.startsWith("/")){
    		text = "/"+text;
    	}
    	if(!text.endsWith("/")){
    		text = text+"/";
    	}
        return text;
    	
    }
    
    

}
